import { reactive } from "vue";
import { page, deleteByIds } from "@/api/recipe";
import {
  add,
  deleteByIds as deleteCollectionByIds,
  deleteByRecipeIds as deleteCollectionByRecipeIds,
} from "@/api/collect";
import { warra } from "@/api/health";
import { page as newsPage } from "@/api/news";
import router from "@/router/index";
import { ElMessage, ElMessageBox, ElNotification } from "element-plus";

const uploadServerUrl = `${import.meta.env.VITE_API_SERVER}`;

export const _$ = reactive({
  state: {
    previewDialogVisible: false,
    dialogImageUrl: "",
  },
  images: [],
  fileList: [],
  warras: [],
  query: {
    requestParam: {
      pageNum: 0,
      pageSize: 10,
      condition: "",
    },
    responseParam: {
      total: 0,
      records: [],
    },
  },
  news: {
    requestParam: {
      pageNum: 0,
      pageSize: 10,
      condition: "",
    },
    responseParam: {
      total: 0,
      records: [],
    },
  },
  delete: {
    requestParam: {
      ids: [],
    },
    responseParam: {},
  },
});

export const $ = {
  onSearch() {
    page(_$.query.requestParam).then((result) => {
      _$.query.responseParam.records.length = 0;
      _$.query.responseParam.records.push(...result.records);
      _$.query.responseParam.total = result.total;

      _$.query.responseParam.records.forEach((item) => {
        item["fileList"] = [];
        item.images.split(",").forEach((url) => {
          item.fileList.push({
            url: `${uploadServerUrl}${url}?Authorization=Bearer ${localStorage.getItem(
              "ats-user-token"
            )}`,
          });
        });
      });
    });
    newsPage(_$.news.requestParam).then((result) => {
      _$.news.responseParam.records.length = 0;
      _$.news.responseParam.records.push(...result.records);
      _$.news.responseParam.total = result.total;
    });
    warra().then((data) => {
      data.warras.forEach((warra) => {
        setTimeout(() => {
          ElNotification({
            title: "通知",
            message: warra,
            type: "error",
          });
        }, 100);
      });
    });
  },

  onDelete() {
    ElMessageBox.confirm("确定删除吗？", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      deleteByIds({
        ids: _$.delete.requestParam.ids.join(","),
      }).then((_) => {
        $.onSearch();
        ElNotification({
          title: "通知",
          message: "删除成功",
          type: "success",
        });
      });
    });
  },

  onCollection(id, item) {
    add({
      recipeId: id,
    }).then((_) => {
      ElNotification({
        title: "通知",
        message: "收藏成功",
        type: "success",
      });
      item.isCollect = true;
    });
  },

  onCancelCollection(id, item) {
    deleteCollectionByRecipeIds({
      ids: id.toString(),
    }).then((_) => {
      ElNotification({
        title: "通知",
        message: "已取消收藏",
        type: "success",
      });
      item.isCollect = false;
    });
  },

  onPreview(uploadFile) {
    _$.state.dialogImageUrl = uploadFile.url;
    _$.state.previewDialogVisible = true;
  },

  onReset() {
    _$.query.requestParam = {
      pageNum: 0,
      pageSize: 10,
      condition: "",
    };
    $.onSearch();
  },

  handleSizeChange(val) {
    _$.query.requestParam.pageSize = val;
    $.onSearch();
  },

  handleCurrentChange(val) {
    _$.query.requestParam.pageNum = val;
    $.onSearch();
  },

  handleSelectionChange(e) {
    _$.delete.requestParam.ids.length = 0;
    e.forEach((item) => {
      _$.delete.requestParam.ids.push(item.id);
    });
  },

  clean() {
    _$.query = {
      requestParam: {
        pageNum: 0,
        pageSize: 10,
        condition: "",
      },
      responseParam: {
        total: 0,
        records: [],
      },
    };
    _$.fileList.length = 0;
    _$.images.length = 0;
    _$.delete = {
      requestParam: {
        ids: [],
        delFlag: "N",
      },
      responseParam: {},
    };
  },
};

export const to = {
  goToIndex() {
    router.push({
      path: "/manage/recipe",
    });
  },

  goToDetail(id) {
    router.push({
      path: "/manage/recipe/detail",
      query: {
        id: id,
      },
    });
  },
};
